package com.lcb.manager.web;

import com.alibaba.fastjson.JSON;
import com.lcb.manager.entity.Diary;
import com.lcb.manager.repository.DiaryRepository;
import com.lcb.manager.vo.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.sql.Date;

@Controller
@RequestMapping(value = "/diary")
public class DiaryController {

    @Autowired
    private DiaryRepository diaryRepository;

    @ResponseBody
    @RequestMapping(value = "/list")
    public String list(@RequestParam int id, @RequestParam(defaultValue = "") String date, @RequestParam int page, @RequestParam int limit) {
        Json result = new Json();
        Pageable pageable = PageRequest.of(page - 1, limit);
        Page<Diary> pageResult;
        if (!date.isEmpty()) {
            String year = date.split("-")[0];
            String month = date.split("-")[1];
            pageResult = diaryRepository.findByDateOrderByDateAsc(id, year, month, pageable);
        } else {
            pageResult = diaryRepository.findByPersonIdOrderByDateAsc(id, pageable);
        }
        result.code = 0;
        result.count = (int) pageResult.getTotalElements();
        result.data = pageResult.getContent();
        return JSON.toJSONStringWithDateFormat(result, "yyyy-MM-dd");
    }

    @RequestMapping(value = "/get_empty_form")
    public String getEmptyForm(ModelMap modelMap) {
        modelMap.put("title", "日记");
        modelMap.put("isEdit", false);
        return "_diary_form";
    }

    @RequestMapping(value = "/get_form")
    public String getForm(Model model, @RequestParam int id) {
        Diary diary = diaryRepository.findById(id).orElse(null);
        model.addAttribute("title", "日记");
        model.addAttribute("isEdit", true);
        if (diary != null) {
            model.addAttribute("vo", diary);
        }
        return "_diary_form";
    }

    @ResponseBody
    @PostMapping("/add")
    public String add(
            @RequestParam("personId") int personId,
            @RequestParam("date") String date,
            @RequestParam("content") String content) throws IOException {
        Json result = new Json();

        Diary diary = new Diary();
        diary.setPersonId(personId);
        diary.setDate(Date.valueOf(date));
        diary.setContent(content);
        diaryRepository.save(diary);
        result.code = 0;
        result.msg = "提交成功！";
        return JSON.toJSONString(result);
    }

    @ResponseBody
    @PostMapping("/edit")
    public String edit(
            @RequestParam("id") int id,
            @RequestParam("date") String date,
            @RequestParam("content") String content) throws IOException {
        Json result = new Json();

        Diary diary = diaryRepository.findById(id).orElse(null);
        if (diary != null) {
            diary.setDate(Date.valueOf(date));
            diary.setContent(content);
            diaryRepository.save(diary);
        }
        result.code = 0;
        result.msg = "提交成功！";
        return JSON.toJSONString(result);
    }

    @ResponseBody
    @RequestMapping(value = "/delete")
    public String delete(@RequestParam int id) {
        Json result = new Json();
        diaryRepository.deleteById(id);
        result.code = 0;
        result.msg = "删除成功！";
        return JSON.toJSONString(result);
    }
}
